﻿Public Class CarHDService

    Public Shared Function GetHDCarList(ByVal parentId As String, ByVal projectId As String, ByVal startTime As String, ByVal endTime As String, ByVal timeValue As String) As IList(Of ProjectInfo.CarHDItem)
        Dim carHDList As IList(Of ProjectInfo.CarHDItem)
        carHDList = New List(Of ProjectInfo.CarHDItem)

        Dim dt As DataSet
        Dim strsql As String = ""
        'strsql += String.Format("select * from tbcchistdata_car_{0}_1 where parentId={3} and UpdateTime between '{1}' and '{2}' and ((HOUR(updatetime)*3600+ MINUTE(updatetime)*60+SECOND(updatetime))- (HOUR('{1}')*3600+ MINUTE('{1}')*60+SECOND('{1}'))) % {4} =0", projectId, startTime, endTime, parentId, timeValue)
        'strsql += String.Format("select * from tbcchistdata_car_{0}_1 where parentId={3} and UpdateTime between '{1}' and '{2}' and ((HOUR('{2}')*3600+ MINUTE('{2}')*60+SECOND('{2}'))- (HOUR('{1}')*3600+ MINUTE('{1}')*60+SECOND('{1}'))) % {4} =0", projectId, startTime, endTime, parentId, timeValue)
        strsql += String.Format("select * from tbcchistdata_car_{0}_1 where parentId={3} and ((HOUR(updatetime)*3600+ MINUTE(updatetime)*60+SECOND(updatetime))- (HOUR('{1}')*3600+ MINUTE('{1}')*60+SECOND('{1}'))) % {4} =0", projectId, startTime, endTime, parentId, timeValue)
        dt = SqlHelper.ExecuteDataset(strsql, Nothing)

        For Each r As DataRow In dt.Tables(0).Rows
            Dim car As New ProjectInfo.CarHDItem

            car.ParentId = SqlHelper.GetDbData(r("ParentId"))
            car.UpdateTime = SqlHelper.GetDbData(r("UpdateTime"))
            car.Latitude_dir = SqlHelper.GetDbData(r("Latitude_dir"))
            car.Latitude = SqlHelper.GetDbData(r("Latitude"))
            Dim count As Integer = car.Latitude.ToString.Length

            If count > 0 Then
                'msg.Show("car.Latitude.IndexOf('.')=" + car.Latitude.IndexOf(".").ToString)
                Dim start As String = car.Latitude.Substring(0, car.Latitude.IndexOf(".")) '找到这一点

                If start.Equals("-300") Then
                    car.Latitude = "---"
                    'Exit For

                Else

                    Dim first As String = ""
                    If start.Length > 0 Then

                        If start.Length >= 2 Then
                            If start.Length = 2 Then
                                first = start.Substring(0, start.Length - 2) + "00°"
                            Else
                                first = start.Substring(0, start.Length - 2) + "°"

                            End If
                            Dim secoud As String = start.Substring(start.Length - 2)
                            'msg.Show("car.Latitude.IndexOf('.')=" + car.Latitude.IndexOf(".").ToString)
                            'msg.Show("count=" + count.ToString)
                            Dim endString As String = car.Latitude.Substring(car.Latitude.IndexOf("."))
                            car.Latitude = first + secoud + endString + "'N"
                        Else
                            car.Latitude = start + "°00.00'N"
                        End If



                    Else
                        car.Latitude = "---"

                    End If
                End If


            Else
                car.Latitude = "---"

            End If


            car.Longitude_dir = SqlHelper.GetDbData(r("Longitude_dir"))
            car.Longitude = SqlHelper.GetDbData(r("Longitude"))

            Dim countL As Integer = car.Longitude.ToString.Length

            If countL > 0 Then
                Dim start As String = car.Longitude.Substring(0, car.Longitude.IndexOf(".")) '找到这一点
                If start.Equals("-300") Then
                    car.Longitude = "---"

                Else

                    Dim first As String = ""
                    If start.Length > 0 Then

                        If start.Length >= 2 Then
                            If start.Length = 2 Then
                                first = start.Substring(0, start.Length - 2) + "00°"
                            Else
                                first = start.Substring(0, start.Length - 2) + "°"

                            End If
                            Dim secoud As String = start.Substring(start.Length - 2)

                            Dim endString As String = car.Longitude.Substring(car.Longitude.IndexOf("."))
                            car.Longitude = first + secoud + endString + "'E"
                        Else
                            car.Longitude = start + "°00.00'E"
                        End If



                    Else
                        car.Longitude = "---"

                    End If

                End If
            Else
                car.Longitude = "---"

            End If

            car.AlarmStatus = SqlHelper.GetDbData(r("AlarmStatus"))

            car.AI1 = SqlHelper.GetDbData(r("AI1"))
            car.AI2 = SqlHelper.GetDbData(r("AI2"))
            car.AI3 = SqlHelper.GetDbData(r("AI3"))
            car.AI4 = SqlHelper.GetDbData(r("AI4"))

            '设置最大温度
            If car.AI1 > car.AI2 AndAlso car.AI1 > car.AI3 Then
                car.MaxTemper = car.AI1
            ElseIf car.AI2 > car.AI1 AndAlso car.AI2 > car.AI3 Then
                car.MaxTemper = car.AI2
            Else
                car.MaxTemper = car.AI3
            End If
            '设置最小温度
            If car.AI1 < car.AI2 AndAlso car.AI1 < car.AI3 Then
                car.MinTemper = car.AI1
            ElseIf car.AI2 < car.AI1 AndAlso car.AI2 < car.AI3 Then
                car.MinTemper = car.AI2
            Else
                car.MinTemper = car.AI3

            End If
            '平均温度
            car.AvgTemper = System.Math.Round(((Convert.ToDecimal(car.AI1) + Convert.ToDecimal(car.AI2) + Convert.ToDecimal(car.AI3)) / 3), 2, MidpointRounding.AwayFromZero)
            carHDList.Add(car)

        Next
        Return carHDList
    End Function
    ''' <summary>
    ''' 查找数据中最后一条
    ''' </summary>
    ''' <param name="carPrp"></param>
    ''' <param name="pid"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetLastData(ByVal carPrp As String, ByVal pid As Integer) As DateTime
        Dim strSql As String = ""
        strSql += String.Format("select max(UpdateTime) from TbccHistData_Car_{0}_1 where ParentId={1} ", carPrp, pid)
        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Return DateTime.MinValue
        End If

        Return obj

    End Function

    ''' <summary>
    ''' 上传对应启停记录数据
    ''' </summary>
    ''' <param name="pi"></param>
    ''' <param name="StartStopid"></param>
    ''' <param name="carHD"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>

    Public Shared Function SetHDData(ByVal pi As String, ByVal StartStopid As Integer, ByVal carHD() As CarWebServiceAdd.CarHisView) As Boolean
        Dim strSql As String
        Dim tic As New TBCC.Utility.TranstrationItem
        strSql = String.Format("Insert Into TbccHistData_Car_{0}_1(ParentId,UpdateTime,AI1,AI2,AI3,AI4,Latitude_dir,Latitude,Longitude_dir,Longitude,AlarmStatus) Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10);-- ", pi)

        For k As Integer = 0 To carHD.Length - 1
            Dim CarHDItem As CarWebServiceAdd.CarHisView
            CarHDItem = carHD(k)

            If CarHDItem.updateTime = Nothing Then
                Exit Function
            End If
            Dim p(10) As MySql.Data.MySqlClient.MySqlParameter
            For i As Integer = 0 To p.GetUpperBound(0)
                p(i) = New MySql.Data.MySqlClient.MySqlParameter
                p(i).ParameterName = "p" + i.ToString
            Next
            p(0).Value = StartStopid
            p(1).Value = CarHDItem.updateTime
            p(2).Value = CarHDItem.ai1
            p(3).Value = CarHDItem.ai2
            p(4).Value = CarHDItem.ai3
            p(5).Value = CarHDItem.ai4
            p(6).Value = CarHDItem.latitude_dir
            p(7).Value = CarHDItem.latitude
            p(8).Value = CarHDItem.longitude_dir
            p(9).Value = CarHDItem.longitude
            p(10).Value = CarHDItem.alarmStatus

            tic.Add(strSql & k, p)

        Next
        

        Return SqlHelper.ExecuteTranstration(tic)
    End Function
End Class
